package org.xserver.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public final class ClasspathUtil {
	public static String getResourcePath() {
		URL url = ClasspathUtil.class.getClassLoader().getResource("");
		return url.getPath();
	}

	public static String getResourceContent(String resourceName)
			throws IOException {
		InputStream in = ClasspathUtil.class.getClassLoader()
				.getResourceAsStream(resourceName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));

		StringBuffer sb = new StringBuffer();
		String line = "";
		while ((line = reader.readLine()) != null) {
			sb.append(line).append("\n");
		}

		return sb.substring(0, sb.length() - 1);
	}

	public static InputStream getResourceAsStream(String resourceName) {
		return ClasspathUtil.class.getClassLoader().getResourceAsStream(
				resourceName);
	}

	public static List<String> getResourceAsList(String resourceName)
			throws IOException {
		InputStream in = ClasspathUtil.class.getClassLoader()
				.getResourceAsStream(resourceName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));

		List<String> list = new ArrayList<String>();
		String line = "";
		while ((line = reader.readLine()) != null) {
			list.add(line);
		}

		return list;
	}

	public static void main(String[] args) throws IOException {
		System.out.println(getResourcePath());
		System.out.println(getResourceContent("applicationContext.xml"));
	}
}
